package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.*;
import Entity.Administrator;
import Entity.Student;
import Entity.Teacher;
/**
 * Servlet implementation class LoginServlet
 */
//@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//解决乱码
		response.setContentType("text/html;charset=utf-8");//解决乱码
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter(); //把编码设置放到输出流获取之前
		String userName=request.getParameter("userName");		
		String pwd=request.getParameter("pwd");
		String status=request.getParameter("status");
		HttpSession session=request.getSession();
		//out.print(userName+pwd+status);
		//out.print(status);
		session.setAttribute("session_status", status);
		System.out.print(userName+pwd+status);
		if(status==null) {
			out.print("<script type='text/javascript'>alert('请选择登录身份')</script>");
		}
		if(status!=null) {
		if(status.equals("student")) {
			if(new StudentsInformationDao().queryStudentByNo(userName) instanceof Student) {
				if(pwd.equals(new StudentsInformationDao().queryStudentByNo(userName).getPwd())) {
					//out.print("登录成功");
					out.print("<script type='text/javascript'>alert('登录成功！学生')</script>");
					System.out.print("登录成功 学生");
					session.setAttribute("session_userName",userName);
					String loginedUserName=(String) session.getAttribute("session_userName");
					out.print(loginedUserName);
				}
				else {
					//out.print("密码错误");
					out.print("<script type='text/javascript'>alert('用户名或密码错误！')</script>");
					System.out.print("密码错误");
				}
			}
			else {
				//out.print("用户名不存在");
				out.print("<script type='text/javascript'>alert('用户名不存在！')</script>");
				System.out.print("用户名不存在");
			}
		}
		
		else if(status.equals("teacher")) {
			if(new TeachersInformationDao().queryTeacherByNo(userName) instanceof Teacher) {
				if(pwd.equals(new TeachersInformationDao().queryTeacherByNo(userName).getPwd())) {
					out.print("<script type='text/javascript'>alert('登录成功！教师')</script>");
					System.out.print("登录成功 教师");
					session.setAttribute("session_userName",userName);
					String loginedUserName=(String) session.getAttribute("session_userName");
					out.print(loginedUserName);
				}
				else {
				out.print("<script type='text/javascript'>alert('用户名或密码错误！')</script>");
				System.out.print("密码错误");
				}
			}
			else {
				//out.print("用户名不存在");
				out.print("<script type='text/javascript'>alert('用户名不存在！')</script>");
				System.out.print("用户名不存在");
			}
		}
		
		else if(status.equals("administrator")){
			if(new AdministratorDao().queryAdminByUserName(userName) instanceof Administrator) {
				if(pwd.equals(new AdministratorDao().queryAdminByUserName(userName).getPwd())) {
					out.print("<script type='text/javascript'>alert('登录成功！管理员')</script>");
					session.setAttribute("session_admin", userName);
					String loginedUserName=(String) session.getAttribute("session_admin");
					out.print(loginedUserName); 
					//response.sendRedirect("JSP_administrator/administratorPage.jsp");
				}
				else {
					//out.print("密码错误");
					out.print("<script type='text/javascript'>alert('用户名或密码错误！')</script>");
					System.out.print("密码错误");
				}
			}
			else {
				//out.print("用户名不存在");
				out.print("<script type='text/javascript'>alert('用户名不存在！')</script>");
				System.out.print("用户名不存在");
			}
			}
		
		}
		//request.setAttribute("r_userName", arg1);
		out.flush();
		out.close();
	}

}
